package com.zhongxi.guoyuan.fragment.beautifulStoreOrder;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.beautifulStoreOrder.StoreOrderHasNoDealAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseFragment;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.entity.beautifulOrder.BeautifulStoreOrderData;
import com.zhongxi.guoyuan.entity.beautifulOrder.BeautifulStoreOrderInfoResult;
import com.zhongxi.guoyuan.entity.beautifulOrder.BeautifulStoreOrderResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class StoreOrderHasNoDealFragment extends BaseFragment implements AdapterView.OnItemClickListener, OnRefreshListener2<ListView> {

    private PullToRefreshListView lvList;
    private StoreOrderHasNoDealAdapter adapter;
    private String type;
    private List<BeautifulStoreOrderData> data = new ArrayList<BeautifulStoreOrderData>();
    private BeautifulStoreOrderResult result;
    private ListView listView;
    private JSONObject shopInfo;

    private void getOrderData() {
        type = "order";
        String str = "&page=" + pageNum;
        getServer(Url.TOBE_CONFIRMED, null, str, false, null, "get");
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_beautyorder_has_no_apply, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        lvList = (PullToRefreshListView) getView().findViewById(R.id.lvList);
        lvList.setMode(PullToRefreshBase.Mode.BOTH);
        adapter = new StoreOrderHasNoDealAdapter(context, data,this);
        lvList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        super.initData();
        getOrderData();
        TextView tv = new TextView(context);
        tv.setGravity(Gravity.CENTER);
        tv.setText("亲,你还没有待受理数据哟!");
        tv.setTextSize(20);
        //当界面为空的时候显示的视图
        lvList.setEmptyView(tv);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden)
        getOrderData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        lvList.setOnItemClickListener(this);
        lvList.setOnRefreshListener(this);
        listView = lvList.getRefreshableView();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position - 1 == adapter.getmExpandedMenuPos()) {
            adapter.setmExpandedMenuPos(-1);
        } else {
            adapter.setmExpandedMenuPos(position - 1);

        }
        adapter.notifyDataSetChanged();
        listView.setSelection(position);
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try {
            if ("order".equals(type)) {
                result = gson.fromJson(response, BeautifulStoreOrderResult.class);
                MyApplication.getInstance().setSpValue(result.getSafeobj());
                if (pageNum == 1) {
                    data.clear();
                }
                JSONObject jsonObject = new JSONObject(response);
                if (result.getData() != null && result.getData().size() > 0) {
                    data.addAll(result.getData());
                    shopInfo = jsonObject.optJSONObject("shopInfo");
                    adapter.setShopInfo(shopInfo);
                }
                adapter.notifyDataSetChanged();
                lvList.onRefreshComplete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        try {
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if (null != result.getSafeobj() && !"".equals(result.getSafeobj()))
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            if (type.equals("order")) {
                if (result.getCode() == 1201) {
                    showShortToast("您预约的店铺已关闭");
                }
            }

            lvList.onRefreshComplete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum = 1;
        getOrderData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        pageNum++;
        getOrderData();
    }
}
